<?php
class Mability extends CI_Model {
    protected $_table;
    public function __construct() {
        parent::__construct();
        $this->_table="ability";
    }
    
    public function listAll($amount, $start) {
        $query=$this->db->query("select a.*, prov.prov_name, pro.pro_title, pro.pro_img from ability as a
                                inner join providers as prov on prov.prov_id = a.prov_id
                                inner join products as pro on pro.pro_id = a.pro_id
                                limit $start, $amount");
        if($query->num_rows()>0) return $query->result_array(); else return false;
    }
    
    public function countAll() {
        return $this->db->count_all($this->_table);
    }
    
    public function getById($id) {
        $query=$this->db->query("SELECT a . * , prov.prov_name, pro.pro_title, pro.pro_img
                                FROM ability AS a
                                INNER JOIN providers AS prov ON prov.prov_id = a.prov_id
                                INNER JOIN products AS pro ON pro.pro_id = a.pro_id
                                WHERE a.ability_id =$id");
        if($query->num_rows()>0) return $query->row_array(); else return false;
    }
    
    public function getByProvIdAndProId($prov_id, $pro_id) {
        $this->db->where("prov_id", $prov_id);
        $this->db->where("pro_id", $pro_id);
        $query=$this->db->get($this->_table);
        if($query->num_rows()>0) return $query->row_array(); else return false;
    }
    
    public function search($prov, $pro, $from, $to, $amount, $start) {
        $p=$pr=$u="'1=1'";
        if($prov!=0) $p="a.prov_id = '".$prov."'";
        if($pro!="") $pr="pro.pro_title like '%".$pro."%'";
        if($from!="" and $to!="") $u="ability_unit_price between $from and $to";
        $str="select a.*, prov.prov_name, pro.pro_title, pro.pro_img from ability as a
                inner join providers as prov on prov.prov_id = a.prov_id
                inner join products as pro on pro.pro_id = a.pro_id
                where $p and $pr and $u
                limit $start, $amount";
        $query=$this->db->query($str);
        if($query->num_rows()>0) return $query->result_array(); else return false;
    }
    
    public function countAllResults($prov, $pro, $from, $to) {
        $p=$pr=$u="'1=1'";
        if($prov!=0) $p="a.prov_id = '".$prov."'";
        if($pro!="") $pr="pro.pro_title like '%".$pro."%'";
        if($from!="" and $to!="") $u="ability_unit_price between $from and $to";
        $str="select count(*) as 'count' from ability as a
                inner join providers as prov on prov.prov_id = a.prov_id
                inner join products as pro on pro.pro_id = a.pro_id
                where $p and $pr and $u";
        $query=$this->db->query($str);
        if($query->num_rows()>0) return $query->row_array(); else return false;
    }
    
    public function insert($data) {
        $this->db->insert($this->_table, $data);
    }
    
    public function update($data, $id) {
        $this->db->where("ability_id", $id);
        $this->db->update($this->_table, $data);
    }
    
    public function del($id) {
        $this->db->where("ability_id", $id);
        $this->db->delete($this->_table);
    }
}
?>
